home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Modules / syslogmodule.c < prev    next >
C/C++ Source or Header  |  1998-12-25  |  6KB  |  261 lines

  1. /***********************************************************
  2. Copyright 1994 by Lance Ellinghouse,
  3. Cathedral City, California Republic, United States of America.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the name of Lance Ellinghouse
  12. not be used in advertising or publicity pertaining to distribution 
  13. of the software without specific, written prior permission.
  14.  
  15. LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL, 
  18. INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 
  19. FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
  20. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
  21. WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /******************************************************************
  26.  
  27. Revision history:
  28.  
  29. 95/06/29 (Steve Clift)
  30.   - Changed arg parsing to use PyArg_ParseTuple.
  31.   - Added PyErr_Clear() call(s) where needed.
  32.   - Fix core dumps if user message contains format specifiers.
  33.   - Change openlog arg defaults to match normal syslog behaviour.
  34.   - Plug memory leak in openlog().
  35.   - Fix setlogmask() to return previous mask value.
  36.  
  37. ******************************************************************/
  38.  
  39. /* syslog module */
  40.  
  41. #include "Python.h"
  42.  
  43. #include <syslog.h>
  44. #if defined(AMITCP) || defined(INET225)
  45. #include <proto/socket.h>
  46. #endif
  47. #include "protos/syslogmodule_protos.h"
  48.  
  49. #ifndef INET225
  50. static PyObject * 
  51. syslog_openlog(self, args)
  52.     PyObject * self;
  53.     PyObject * args;
  54. {
  55.     long logopt = 0;
  56.     long facility = LOG_USER;
  57.  
  58.     static PyObject *ident_o = NULL;
  59.  
  60.     Py_XDECREF(ident_o);
  61.     if (!PyArg_ParseTuple(args,
  62.                   "S|ll;ident string [, logoption [, facility]]",
  63.                   &ident_o, &logopt, &facility))
  64.         return NULL;
  65.  
  66.     /* This is needed because openlog() does NOT make a copy
  67.      * and syslog() later uses it.. cannot trash it.
  68.      */
  69.     Py_INCREF(ident_o);
  70.  
  71.     openlog(PyString_AsString(ident_o), logopt, facility);
  72.  
  73.     Py_INCREF(Py_None);
  74.     return Py_None;
  75. }
  76. #endif /* !INET225 */
  77.  
  78. static PyObject * 
  79. syslog_syslog(self, args)
  80.     PyObject * self;
  81.     PyObject * args;
  82. {
  83.     char *message;
  84.     int   priority = LOG_INFO | LOG_USER;
  85.  
  86.     if (!PyArg_ParseTuple(args, "is;[priority,] message string",
  87.                   &priority, &message)) {
  88.         PyErr_Clear();
  89.         if (!PyArg_ParseTuple(args, "s;[priority,] message string",
  90.                       &message))
  91.             return NULL;
  92.     }
  93.     syslog(priority, "%s", message);
  94.     Py_INCREF(Py_None);
  95.     return Py_None;
  96. }
  97.  
  98. #ifndef INET225
  99. static PyObject * 
  100. syslog_closelog(self, args)
  101.     PyObject * self;
  102.     PyObject * args;
  103. {
  104.     if (!PyArg_ParseTuple(args, ""))
  105.         return NULL;
  106.     closelog();
  107.     Py_INCREF(Py_None);
  108.     return Py_None;
  109. }
  110.  
  111. static PyObject * 
  112. syslog_setlogmask(self, args)
  113.     PyObject * self;
  114.     PyObject * args;
  115. {
  116.     long maskpri, omaskpri;
  117.  
  118.     if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
  119.         return NULL;
  120.     omaskpri = setlogmask(maskpri);
  121.     return PyInt_FromLong(omaskpri);
  122. }
  123.  
  124. static PyObject * 
  125. syslog_log_mask(self, args)
  126.     PyObject * self;
  127.     PyObject * args;
  128. {
  129.     long mask;
  130.     long pri;
  131.     if (!PyArg_ParseTuple(args, "l", &pri))
  132.         return NULL;
  133.     mask = LOG_MASK(pri);
  134.     return PyInt_FromLong(mask);
  135. }
  136.  
  137. static PyObject * 
  138. syslog_log_upto(self, args)
  139.     PyObject * self;
  140.     PyObject * args;
  141. {
  142.     long mask;
  143.     long pri;
  144.     if (!PyArg_ParseTuple(args, "l", &pri))
  145.         return NULL;
  146.     mask = LOG_UPTO(pri);
  147.     return PyInt_FromLong(mask);
  148. }
  149.  
  150. /* List of functions defined in the module */
  151.  
  152. static PyMethodDef syslog_methods[] = {
  153.     {"openlog",    syslog_openlog,        METH_VARARGS},
  154.     {"closelog",    syslog_closelog,    METH_VARARGS},
  155.     {"syslog",    syslog_syslog,        METH_VARARGS},
  156.     {"setlogmask",    syslog_setlogmask,    METH_VARARGS},
  157.     {"LOG_MASK",    syslog_log_mask,    METH_VARARGS},
  158.     {"LOG_UPTO",    syslog_log_upto,    METH_VARARGS},
  159.     {NULL,        NULL,            0}
  160. };
  161. #endif /* !INET225 */
  162.  
  163. #ifdef INET225
  164. /* List of functions defined in the module */
  165. /* I-Net 225 only has syslog */
  166.  
  167. static PyMethodDef syslog_methods[] = {
  168.     {"syslog",    syslog_syslog,        METH_VARARGS},
  169.     {NULL,        NULL,            0}
  170. };
  171. #endif /* INET225 */
  172.  
  173. /* Initialization function for the module */
  174.  
  175. static void
  176. ins(d, s, x)
  177.     PyObject *d;
  178.     char *s;
  179.     long x;
  180. {
  181.     PyObject *v = PyInt_FromLong(x);
  182.     if (v) {
  183.         PyDict_SetItemString(d, s, v);
  184.         Py_DECREF(v);
  185.     }
  186. }
  187.  
  188.  
  189. void
  190. initsyslog()
  191. {
  192.     PyObject *m, *d;
  193.  
  194. #if defined(AMITCP) || defined(INET225)
  195.     if(!checksocketlib()) return;
  196. #endif
  197.  
  198.     /* Create the module and add the functions */
  199.     m = Py_InitModule("syslog", syslog_methods);
  200.  
  201.     /* Add some symbolic constants to the module */
  202.     d = PyModule_GetDict(m);
  203.  
  204.     /* Priorities */
  205.     ins(d, "LOG_EMERG",    LOG_EMERG);
  206.     ins(d, "LOG_ALERT",    LOG_ALERT);
  207.     ins(d, "LOG_CRIT",    LOG_CRIT);
  208.     ins(d, "LOG_ERR",    LOG_ERR);
  209.     ins(d, "LOG_WARNING",    LOG_WARNING);
  210.     ins(d, "LOG_NOTICE",    LOG_NOTICE);
  211.     ins(d, "LOG_INFO",    LOG_INFO);
  212.     ins(d, "LOG_DEBUG",    LOG_DEBUG);
  213.  
  214.     /* openlog() option flags */
  215.     ins(d, "LOG_PID",    LOG_PID);
  216.     ins(d, "LOG_CONS",    LOG_CONS);
  217.     ins(d, "LOG_NDELAY",    LOG_NDELAY);
  218.     ins(d, "LOG_NOWAIT",    LOG_NOWAIT);
  219. #ifdef LOG_PERROR
  220.     ins(d, "LOG_PERROR",    LOG_PERROR);
  221. #endif
  222.  
  223.     /* Facilities */
  224.     ins(d, "LOG_KERN",    LOG_KERN);
  225.     ins(d, "LOG_USER",    LOG_USER);
  226.     ins(d, "LOG_MAIL",    LOG_MAIL);
  227.     ins(d, "LOG_DAEMON",    LOG_DAEMON);
  228.     ins(d, "LOG_AUTH",    LOG_AUTH);
  229. #ifdef LOG_SYSLOG
  230.     ins(d, "LOG_SYSLOG",    LOG_SYSLOG);
  231. #endif
  232.     ins(d, "LOG_LPR",    LOG_LPR);
  233. #ifdef LOG_NEWS
  234.     ins(d, "LOG_NEWS",    LOG_NEWS);
  235. #else
  236.     ins(d, "LOG_NEWS",    LOG_MAIL);
  237. #endif
  238. #ifdef LOG_UUCP
  239.     ins(d, "LOG_UUCP",    LOG_UUCP);
  240. #else
  241.     ins(d, "LOG_UUCP",    LOG_MAIL);
  242. #endif
  243. #ifdef LOG_CRON
  244.     ins(d, "LOG_CRON",    LOG_CRON);
  245. #else
  246.     ins(d, "LOG_CRON",    LOG_DAEMON);
  247. #endif
  248.     ins(d, "LOG_LOCAL0",    LOG_LOCAL0);
  249.     ins(d, "LOG_LOCAL1",    LOG_LOCAL1);
  250.     ins(d, "LOG_LOCAL2",    LOG_LOCAL2);
  251.     ins(d, "LOG_LOCAL3",    LOG_LOCAL3);
  252.     ins(d, "LOG_LOCAL4",    LOG_LOCAL4);
  253.     ins(d, "LOG_LOCAL5",    LOG_LOCAL5);
  254.     ins(d, "LOG_LOCAL6",    LOG_LOCAL6);
  255.     ins(d, "LOG_LOCAL7",    LOG_LOCAL7);
  256.  
  257.     /* Check for errors */
  258.     if (PyErr_Occurred())
  259.         Py_FatalError("can't initialize module syslog");
  260. }
  261.